/* 
 * BookingView.java 12.05.2011
 *  
 *  
 * Candidate: Alessandra Otterbach
 * Prometric ID: Algisina1
 * Username: Algisina
 *  
 * Sun Certified Developer for Java 2 Platform, Standard Edition Programming 
 * Assignment (CX-310-252A)
 *  
 * This class is part of the Programming Assignment of the Sun Certified 
 * Developer for Java 2 Platform, Standard Edition certification program, must 
 * not be used out of this context and must be used exclusively by Sun 
 * Microsystems, Inc. 
 */
package suncertify.ps;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionListener;

import suncertify.bs.BusinessService;

/**
 * 
 * The view for presenting the data. All the logic is in class BookingController, the model 
 * is in BookingTableModel.
 * 
 * @author Alessandra Otterbach
 * 
 */
public class BookingView extends JFrame {

    private static final long serialVersionUID = 1L;

    private BusinessService service;

    private final int iw = 4;
    private final Insets formPanelInsets = new Insets(iw, iw, iw, iw);

    private JButton jbtSearch;
    private JButton jbtBook;
    private JButton jbtRefresh;

    private JLabel jlbName;
    private JLabel jlbLocation;
    private JTextField jtxtName;
    private JTextField jtxtLocation;

    private JPanel panelSearchCriteria;
    private JPanel panelTable;
    private JPanel panelButtons;

    private JTable table;
    
    private ListSelectionModel listSelectionModel;

    /**
     * Constructor to start the booking view.
     */
    public BookingView() {

    }

    /**
     * Create the GUI and show it. TODO For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    public void createAndShowGUI() {

	
	Border etchedBorder = new EtchedBorder();
	Border emptyBorder = new EmptyBorder(9, 9, 9, 9);
	Border panelBorder = BorderFactory.createCompoundBorder(emptyBorder,
		etchedBorder);

	GridBagConstraints gbc = new GridBagConstraints();
	gbc.insets = formPanelInsets;
	gbc.fill = GridBagConstraints.BOTH;

	// Start Panel for Search Criterias
	panelSearchCriteria = new JPanel();
	panelSearchCriteria.setLayout(new GridBagLayout());
	panelSearchCriteria.setBorder(panelBorder);

	// Hotel Name
	jlbName = new JLabel("Hotel Name: ");
	gbc.gridx = 0;
	gbc.gridy = 0;
	panelSearchCriteria.add(jlbName, gbc);

	jtxtName = new JTextField();
	jtxtName.setColumns(15);
	gbc.gridx = 1;
	gbc.gridy = 0;
	panelSearchCriteria.add(jtxtName, gbc);

	// Hotel Location
	jlbLocation = new JLabel("Hotel Location: ");
	gbc.gridx = 0;
	gbc.gridy = 1;
	panelSearchCriteria.add(jlbLocation, gbc);

	jtxtLocation = new JTextField();
	jtxtLocation.setColumns(15);
	gbc.gridx = 1;
	gbc.gridy = 1;
	panelSearchCriteria.add(jtxtLocation, gbc);

	jbtSearch = new JButton("Search");
	gbc.gridx = 2;
	gbc.gridy = 1;
	panelSearchCriteria.add(jbtSearch, gbc);

	// Start Table
	panelTable = new JPanel();

	table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	listSelectionModel = table.getSelectionModel();	
	table.setSelectionModel(listSelectionModel);
	panelTable.add(new JScrollPane(table));

	// Buttons
	panelButtons = new JPanel();
	panelButtons.setLayout(new GridBagLayout());

	jbtRefresh = new JButton("Refresh");
	gbc.gridx = 0;
	gbc.gridy = 1;
	panelButtons.add(jbtRefresh, gbc);

	jbtBook = new JButton("Book Now");
	jbtBook.setEnabled(false);
	gbc.gridx = 2;
	gbc.gridy = 1;
	panelButtons.add(jbtBook, gbc);

	add(panelSearchCriteria, BorderLayout.NORTH);
	add(panelTable, BorderLayout.CENTER);
	add(panelButtons, BorderLayout.SOUTH);

	init();
    }

    private void init() {

	this.setTitle("Urly Bird");
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.pack();
	this.setSize(500, 650);

	// Center
	Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	int x = (int) ((d.getWidth() - this.getWidth()) / 2);
	int y = (int) ((d.getHeight() - this.getHeight()) / 2);
	this.setLocation(x, y);
	this.setResizable(false);
	this.setVisible(true);
    }

    
    /**
     * Function for the controller: adds BookListener to book-Button
     */
    public void setBookListener(ActionListener bookListener) {
	this.jbtBook.addActionListener(bookListener);
    }

    /**
     * Function for the controller: adds SearchListener to search-Button
     */
    public void setSearchListener(ActionListener searchListener) {
	this.jbtSearch.addActionListener(searchListener);
	this.jtxtLocation.addActionListener(searchListener);
	this.jtxtName.addActionListener(searchListener);
    }
    
    /**
     * Function for the controller: adds RefreshListener to refresh-Button
     */
    public void setRefreshListener(ActionListener refreshListener) {
	this.jbtRefresh.addActionListener(refreshListener);
    }
    
    /**
     * Function for the controller: adds ListSelectionListener to listSelectionModel
     */
    public void setListSelectionListener(ListSelectionListener listSelectionListener) {
	this.listSelectionModel.addListSelectionListener(listSelectionListener);
    }

    /**
     * Returns the JTable with the data for the controller.
     * 
     * @return table with data.
     */
    public JTable getTable() {
	return this.table;
    }

    /**
     * Sets the table defined in the controller.
     * 
     * @param table
     */
    public void setTable(JTable table) {
	this.table = table;
    }

    /**
     * Returns the BusinessService for the controller.
     * 
     * @return BusinessService
     */
    public BusinessService getBusinessService() {
	return this.service;
    }

    /**
     * Sets the BusinessService defined in the controller.
     * 
     * @param businessService
     */
    public void setBusinessService(BusinessService businessService) {
	this.service = businessService;
    }

    /**
     * Returns search string "name".
     * 
     * @return name
     */
    public JTextField getTxtName() {
	return this.jtxtName;
    }

    /**
     * Returns search string "location".
     * 
     * @return name
     */
    public JTextField getTxtLocation() {
	return this.jtxtLocation;
    }
    
    /**
     * Enables/Disables book-button.
     */
    public void enableBookButton(boolean enable) {
	jbtBook.setEnabled(enable);
    }
}
